﻿using System.Text.Json;
using ARC.YunXinShi.SMSService.Interfaces;
using ARC.YunXinShi.SMSService.Models;
using ARC.YunXinShi.SMSService.Models.SimpleSmsModels;
using System.Web;
namespace ARC.YunXinShi.SMSService.Service.SimpleSmsService
{
    public class SimpleSms : BaseYunXinShi, ISimpleSms
    {
        private const string Url = "http://api.sms.cn/sms/";
        private string _uid;
        private string _pwd;
        public SimpleSms(string uid, string pwd)
        {
            _uid = uid;
            _pwd = pwd;
        }
        public AddAndDeleAndUpdateSimpleTelResponse AddSimpleTemplate(string content, string type, string title = "",
            string dataformat = "")
        {
            string dataSmsParams = "ac=" + "template" + "&uid=" + _uid + "&pwd=" + _pwd + "&title=" + title + "&content=" + HttpUtility.UrlEncode(content) + "&type=" + type + "&dataformat=" + dataformat;
            return JsonSerializer.Deserialize<AddAndDeleAndUpdateSimpleTelResponse>(Post(Url, dataSmsParams));
        }

        public AddAndDeleAndUpdateSimpleTelResponse DeleteSimpleTemplate(string content, string type, string templateId, string delete)
        {
            string dataSmsParams = "ac=" + "template" + "&uid=" + _uid + "&pwd=" + _pwd + "&content=" + HttpUtility.UrlEncode(content) + "&type=" + type +"&templateid="+ templateId + "&delete=" + delete;
            return JsonSerializer.Deserialize<AddAndDeleAndUpdateSimpleTelResponse>(Post(Url, dataSmsParams));
        }

        public AddAndDeleAndUpdateSimpleTelResponse UpdateSimpleTemplate(string content, string type, string templateid)
        {
            string dataSmsParams = "ac=" + "template" + "&uid=" + _uid + "&pwd=" + _pwd + "&content=" + HttpUtility.UrlEncode(content) + "&type=" + type + "&templateid=" + templateid;
            return JsonSerializer.Deserialize<AddAndDeleAndUpdateSimpleTelResponse>(Post(Url, dataSmsParams));
        }

        public QuerySimpleTelResponse QuerySimpleTemplate(string templateid, string encode = "gbk")
        {
            string dataSmsParams = "ac=templatestatus" + "&uid=" + _uid + "&pwd=" + _pwd + "&templateid=" + templateid+ "&encode="+ encode;
            return JsonSerializer.Deserialize<QuerySimpleTelResponse>(Post(Url, dataSmsParams.ToLower()));
        }

        public QuerySimpleTemplateReCodeResponse QueryTemplateReCode(string page = "1", string encode = "gbk")
        {
            string dataSmsParams = "ac=" + "templatequery" + "&uid=" + _uid + "&pwd=" + _pwd + "&page=" + page + "&encode=" + encode;
            return JsonSerializer.Deserialize<QuerySimpleTemplateReCodeResponse>(Post(Url, dataSmsParams));
        }

        public BaseResponse SendSimple(string mobile, string content, string template= "", string time = "", string mobileids = "")
        {
            string dataSmsParams = "ac=" + "send" + "&uid=" + _uid + "&pwd=" + _pwd + "&mobile=" + mobile + "&content=" + content + "&template=" + template + "&time=" + time + "&mobileids=" + mobileids;
            return JsonSerializer.Deserialize<BaseResponse>(Post(Url, dataSmsParams));
        }

        public BaseResponse SendSimple(string mobile, string content, string time = "", string mobileids = "")
        {
            string dataSmsParams = "ac=" + "send" + "&uid=" + _uid + "&pwd=" + _pwd + "&mobile=" + mobile + "&content=" + content + "&time=" + time + "&mobileids=" + mobileids;
            return JsonSerializer.Deserialize<BaseResponse>(Post(Url, dataSmsParams));
        }

        public SendedSimpleResponse SendedSimpleCount(string cmd)
        {
            string dataSmsParams = "ac=" + "number" + "&uid=" + _uid + "&pwd=" + _pwd + "&cmd=" + cmd;
            return JsonSerializer.Deserialize<SendedSimpleResponse>(Post(Url, dataSmsParams));
        }

        public SendedSimpleStatusResponse SendedSimpleStatus()
        {
            string dataSmsParams = "ac=" + "status" + "&uid=" + _uid + "&pwd=" + _pwd;
            return JsonSerializer.Deserialize<SendedSimpleStatusResponse>(Post(Url, dataSmsParams));
        }
    }
}
